package thread.lock;

import thread.ThreadUtils;

public class CAS_Test {
    public static  int COUNT = 0;
    public synchronized static boolean CompareAndSwap(int expect,int update){
        if(expect==COUNT){
            COUNT = update;
            return true;
        }

        return false;
    }

    public static void main(String[] args) {
        for (int i = 0; i < 100; i++) {
            new Thread(()->{
                ThreadUtils.sleep(1);
                while (!CompareAndSwap(COUNT,COUNT+1)){}
            }).start();
            ThreadUtils.sleep(1000);
            System.out.println(COUNT);
        }
    }

}
